﻿//显示蛇
package com.victim.snake.graphics
{
	import com.victim.snake.snake.Snake;
	import com.victim.snake.snake.Body;
	import flash.display.Graphics;	
	import flash.utils.Dictionary;
	import flash.display.Sprite;
	import com.victim.snake.snake.Food;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import com.victim.snake.user.User;
	import com.victim.snake.util.GraphicsToll;
	import flash.display.Bitmap;
	import com.victim.snake.snake.SnakeStyle;
	import flash.geom.Matrix;
	import flash.filters.GlowFilter;

	//蛇显示类
	public class SnakeGraphics
	{
		var snakeGraphicsFlag:uint = 0;//双缓冲始用标志
		private var sprites:Array=new Array();//蛇要显示在其之上的双缓冲画布
		private var snakes:Dictionary;//要在画布上显示的蛇的列表
		private var foods:Array=new Array();//要在画布上显示的食物列表
		private var bodyAlpha:Number=1;	//透明度，用来闪烁蛇
		private var bodyAlphaType:Boolean=false;	//透明度增加或减少标志	true:增加	false:减少
		private var bodyAlphaTimer:Timer=new Timer(20,300);	//闪烁蛇计数器
		private var matrix:Matrix=new Matrix();	//用于蛇身体旋转
		
		static var redGlowFilter:GlowFilter=new GlowFilter(0xff0000,1,9,9,4,1,false,false);
		
		
		//构造函数
		public function SnakeGraphics()
		{						
			this.sprites[0]=new Sprite();	//初使化画布
			this.sprites[1]=new Sprite();	//初使化画布
			
			
			//画边界
/*			Sprite(this.sprites[0]).graphics.beginFill(0x000000,1);
			GraphicsToll.drawRectBorder(Sprite(this.sprites[0]).graphics,0,0,800,480);
			Sprite(this.sprites[0]).graphics.endFill();
			Sprite(this.sprites[1]).graphics.beginFill(0x000000,1);
			GraphicsToll.drawRectBorder(Sprite(this.sprites[1]).graphics,0,0,800,480);
			Sprite(this.sprites[1]).graphics.endFill();			*/
			
			
			//snakes=new Dictionary();//初使化蛇列表
			
			//添加闪烁蛇方法
			bodyAlphaTimer.addEventListener(TimerEvent.TIMER,flicker);
			bodyAlphaTimer.start();
		}
		
		//闪烁蛇方法，由bodyAlphaTimer调用
		public function flicker(event:TimerEvent):void{
			if(bodyAlphaType){
				bodyAlpha+=0.1;
			}else{
				bodyAlpha-=0.1;
			}
		
			if(bodyAlpha<0){
				bodyAlphaType=true;
			}else if(bodyAlpha>1){
				bodyAlphaType=false;
			}
			//trace(bodyAlpha);
			if(bodyAlphaTimer.currentCount==300){
				bodyAlpha=1;
			}
		}

		//设置要显示的食物列表
		public function setFoods(foods:Array):void
		{
			this.foods=foods;
		}
		
		//将食物添加到要显示的蛇列表
		public function addFood(food:Food):void
		{
			this.foods.push(food);
		}
		
		//删除食物
		public function removeFood(foodId:int):void
		{
			for(var i:int=0;i<this.foods.length;i++){
				if(this.foods[i]!=null&&Food(this.foods[i]).getId()==foodId){
					this.foods.splice(i,1);
				}
			}
			
		}
		
		//设置要显示的蛇列表
		public function setSnakesDictionary(snakes:Dictionary):void
		{
			this.snakes=snakes;
		}
		
		//将蛇添加到要显示的蛇列表
		public function addSnake(snake:Snake):void
		{
			snakes[snake.getName()] = snake;
		}
		
		//在画布上绘制图形
		public function draw():void
		{			
			snakeGraphicsFlag = snakeGraphicsFlag == 0 ? 1:0;
			Sprite(this.sprites[snakeGraphicsFlag]).graphics.clear();
			
			for each (var food:Food in this.foods)
			{
				showFood(food);	//画食物
			}
			
			//循环列表中的每一条蛇
			for each (var snake:Snake in snakes)
			{
				showSnake(snake);	//画蛇
			}
			
			
		}
		
		//画蛇
		private function showSnake(snake:Snake):void
		{
			//是否当前玩家蛇，用于闪烁
			var flickerFlag:Boolean=false;			
			if(User.getSnake()==snake){
				flickerFlag=true;
			}
			
			//循环蛇身体列表画出蛇身体
			for each (var body:Body in snake.getBodys())
			{				
				if(body==snake.getHead()){
					showSnakeBody(body,flickerFlag,snake.getStyle(),1);	//画蛇头
				}else if(body==snake.getTail()){
					showSnakeBody(body,flickerFlag,snake.getStyle(),2);		//画蛇尾
				}else if(body.getIsTrun()){
					showSnakeBody(body,flickerFlag,snake.getStyle(),3);		//转向身体
				}else{
					showSnakeBody(body,flickerFlag,snake.getStyle(),0);	//画蛇身体
				}
			}
		}
		
		//画蛇身体  flickerFlag是否闪烁标志(是否当前玩家蛇)  style表示蛇的样式    block表示身体是头、尾或中间段(0中间段，1头，2尾,3转角)
		private function showSnakeBody(body:Body,flickerFlag:Boolean,style:SnakeStyle,block:uint=0):void
		{
			var graphics:Graphics=Sprite(this.sprites[snakeGraphicsFlag]).graphics;
			
			//是否闪烁(是否当前玩家蛇)
			/*if(flickerFlag){
				graphics.beginFill(body.getColor(),bodyAlpha);
			}else{
				graphics.beginFill(body.getColor(),1);
			}
			graphics.drawRect(body.getX(),body.getY(),body.getWidth(),body.getHeight());
			graphics.endFill();			*/
			
			switch(body.getDirection()){
				case 37:
					//matrix.d=-1;
					matrix.rotate((0/180)*Math.PI);
					break;
				case 38:
					//matrix.d=-1;
					matrix.rotate((90/180)*Math.PI);
					break;
				case 39:
					matrix.d=-1;
					matrix.rotate((180/180)*Math.PI);
					break;
				case 40:
					
					matrix.rotate((270/180)*Math.PI);
					break;
				default:
					matrix.rotate((0/180)*Math.PI);
					break;
			}
			
			switch(block){
				case 0:
					var filtersArray:Array=style.bodyImageVector[0].filters;
					filtersArray[0]=SnakeGraphics.redGlowFilter;
					style.bodyImageVector[0].filters=filtersArray;
					
					graphics.beginBitmapFill(style.bodyImageVector[0].bitmapData,matrix);
					break;
				case 1:
					graphics.beginBitmapFill(style.headImage.bitmapData,matrix);
					break;
				case 2:
					graphics.beginBitmapFill(style.tailImage.bitmapData,matrix);
					break;
				case 3:	//转向身体
					matrix.a=1;
					matrix.b=0;
					matrix.c=0;
					matrix.d=1;
					//判断前一节转向
					switch(body.getPrvDirection()){
						case 37:
							if(body.getDirection()==38){
								matrix.rotate((180/180)*Math.PI);
							}else{
								matrix.a=-1;
							}
							break;
						case 38:
							if(body.getDirection()==39){
								matrix.rotate((270/180)*Math.PI);
							}
							break;
						case 39:														
							if(body.getDirection()==38){
								matrix.rotate((90/180)*Math.PI);
							}
							break;
						case 40:
							if(body.getDirection()==39){
								matrix.rotate((180/180)*Math.PI);
							}else{
								matrix.rotate((90/180)*Math.PI);
							}
							break;
					}
					graphics.beginBitmapFill(style.trunImage.bitmapData,matrix);
					break;
				default:
					graphics.beginBitmapFill(style.bodyImageVector[0].bitmapData,matrix);
					break;
			}
			graphics.drawRect(body.getX(),body.getY(),body.getWidth(),body.getHeight());
			graphics.endFill();
			
			matrix.a=1;
			matrix.b=0;
			matrix.c=0;
			matrix.d=1;
		}
		
		//画食物
		private function showFood(food:Food):void
		{
			var graphics:Graphics=Sprite(this.sprites[snakeGraphicsFlag]).graphics;
			graphics.beginBitmapFill(food.getBitmap().bitmapData,matrix);
			graphics.drawRect(food.getX(),food.getY(),food.getWidth(),food.getHeight());
			graphics.endFill();			
		}
		
		//返回最新绘图
		public function getGraphics():Sprite{
			//trace(snakeGraphicsFlag);
			return Sprite(this.sprites[snakeGraphicsFlag]);
		}


		public function getFoods():Array{
			return this.foods;
		}

	}
}